#define __SYSTEM_C__


#include "system.h"
#include "nr_micro_shell.h"

void Cpu_Init(void)
{
	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4);
}


void Sys_Init(void)
{
   Cpu_Init();
   SysTick_Init();

#if LED_ENABLE
	Led_Init();
#endif
	
#if BEEP_ENABLE
	Beep_Init();
#endif

#if USART1_ENABLE
	Usart1_Init(); 
#endif
	
#if USART2_ENABLE
	Usart2_Init();
#endif

#if USART3_ENABLE
	Usart3_Init();
	Usart3_SendString((unsigned char*)"uart3 init\r\n");
#endif

#if ADC_ENABLE
	Adc_Init();
#endif

#if DAC_ENABLE
	Dac_Init();
#endif

#if BUTTON_ENABLE
	Button_Init();
#endif

#if TIMER2_ENABLE
	Timer2_Init(7199,10000);
#endif

#if PWM_ENABLE
	PWM_Init();
#endif

#if UART4_ENABLE
	Uart4_Init();
	// Uart4_SendString((unsigned char*)"uart4 init\r\n");
#endif

#if UART5_ENABLE
	Uart5_Init();
	Uart5_SendString((unsigned char*)"uart5 init\r\n");
#endif

#if	I2C_ENABLE		
	I2cGpioInit();
#endif

#if AT24C02_ENABLE  
	At24c02Init();
	// AT24C02Test();
#endif

#if MENU_ENABLE
	MainPage();
#endif

#if LCD_ENABLE
	Lcd_Init();			
	LCD_Clear(WHITE);
	BACK_COLOR=WHITE;
	LCD_Test();
#endif

#if W25QXX_SFUD_ENABLE
	sfud_init();
	w25qxx_test();
#endif

#if ((FATFS_ENABLE) && (W25QXX_SFUD_ENABLE))
	spi_flash_mkfs_and_mount(); // 用于初始化文件系统
#endif

#if ((FATFS_ENABLE) && (W25QXX_SFUD_ENABLE) &&(USB_MASS_STORAGE_ENABLE))
	usb_mass_storage_init();
#endif

#if INFLASH_ENABLE
#endif

#if TIMER4_CAPTURE_ENABLE
	Tim4Init();
#endif




// //FATFS fs;					/* FatFs文件系统对象 */
// FIL fobj = {0};					/* 文件对象 */
// UINT fnum = {0};         			/* 文件成功读写数量 */
// FRESULT f_res = {0};     			/* 文件操作结果 */

// f_res = f_open(&fobj, "1:/Test01.txt", FA_OPEN_ALWAYS | FA_WRITE | FA_READ);  // 通过追加模式打开的文件

// if ( f_res == FR_OK )
// {
// 	char str[256] = "成功了, 孩子! ";
// 	f_write(&fobj, str,strlen(str), &fnum);
// 	// f_lseek (&fobj, f_size(&fobj));
// 	f_sync(&fobj);
// 	printf("Store sensor data in SPI FLASH successfully!.\r\n");
// 	f_close(&fobj);
// }
// else
// {	
// 	printf("Failed to open 1:/sensor.csv create SPI FLASH File.\r\n");
// }

// f_close(&fobj);





#if XX_ENABLE


#endif 
	// G_Variables_AT24C02.PWM01_Compare = 10;
	// G_Variables_AT24C02.PWM02_Compare = 20;
	// memcpy(G_Variables_AT24C02.Test_str, "It is OK", 8);

	// At24c02Write(0, (uint8 *)&G_Variables_AT24C02, sizeof(AT24C02));




	// AT24C02 *p = (AT24C02 *)malloc(sizeof(AT24C02));

	// At24c02Read(0, (uint8 *)p, sizeof(AT24C02));
	// printf("\r\n %s \r\n", p->Test_str);
	// printf("\r\n %d \r\n", p->PWM01_Compare);
	// printf("\r\n %d \r\n", p->PWM02_Compare);
	// free(p);

	At24c02Read(0, (uint8 *)&G_Variables_AT24C02, sizeof(AT24C02));
	// printf("\r\n %s \r\n", G_Variables_AT24C02.Test_str);
	printf("\r\n 通道1占空比%d \r\n", G_Variables_AT24C02.PWM01_Compare);
	printf("\r\n 通道2占空比%d \r\n", G_Variables_AT24C02.PWM02_Compare);


	Set_PWM01_Compare(G_Variables_AT24C02.PWM01_Compare);
	Set_PWM02_Compare(G_Variables_AT24C02.PWM02_Compare);

	// TIM_SetCompare1(TIM3, G_Variables_AT24C02.PWM01_Compare);
	// TIM_SetCompare2(TIM3, G_Variables_AT24C02.PWM02_Compare);
	
	
	
	
	
	MyRTC_Init();
	// MyRTC_SetTime();
	MyRTC_ReadTime();
	for (uint8_t i = 0; i < 6; i++)
	{
		printf("%d:", MyRTC_Time[i]);
	}
	printf("\n");
	printf("时间初始化成功\n");

	
	
	
	
	
	
	
	
	
	
	
	
	


	App_Task_Init();

}


void Sys_Run(void)
{
	Task_Scheduling();
	Msg_Process();
}


